package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduDepartmentDTO;
import com.zenith.edu.po.vo.EduDepartmentVO;
import com.zenith.edu.service.EduDepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/department")
@Tag(name = "院系管理", description = "院系管理接口")
public class EduDepartmentController {
    
    @Autowired
    private EduDepartmentService eduDepartmentService;
    
    @SecurePostMapping(value = "createDepartment",roles = "ROLE_ADMIN",permission = "department:create")
    @Operation(summary = "创建院系")
    public Result<EduDepartmentVO> createDepartment(@RequestBody EduDepartmentDTO dto) {
        EduDepartmentVO departmentVO = eduDepartmentService.createDepartment(dto);
        return Result.success(departmentVO);
    }
    
    @SecurePostMapping(value = "getDepartmentById",roles = "ROLE_ADMIN",permission = "department:get")  
    @Operation(summary = "根据ID获取院系详情")
    public Result<EduDepartmentVO> getDepartmentById(@PathVariable Long id) {
        EduDepartmentVO departmentVO = eduDepartmentService.getDepartmentById(id);
        if (departmentVO == null) {
            return Result.fail("院系不存在");
        }
        return Result.success(departmentVO);
    }
    
    @SecurePostMapping(value = "updateDepartment",roles = "ROLE_ADMIN",permission = "department:update")
    @Operation(summary = "更新院系信息")
    public Result<EduDepartmentVO> updateDepartment(@RequestBody EduDepartmentDTO dto) {
        EduDepartmentVO departmentVO = eduDepartmentService.updateDepartment(dto);
        return Result.success(departmentVO);
    }
    
    @SecurePostMapping(value = "deleteDepartment",roles = "ROLE_ADMIN",permission = "department:delete")
    @Operation(summary = "删除指定院系")
    public Result<Void> deleteDepartment(@PathVariable Long id) {
        boolean result = eduDepartmentService.deleteDepartment(id);
        if (!result) {
            return Result.fail("院系删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "getDepartmentList",roles = "ROLE_ADMIN",permission = "department:read")
    @Operation(summary = "获取院系列表")
    public Result<List<EduDepartmentVO>> getDepartmentList() {
        List<EduDepartmentVO> departmentList = eduDepartmentService.getDepartmentList();
        return Result.success(departmentList);
    }
}